<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\facade\Db;

class DataContentChipTemplate extends AdminBase
{
    public function index()
    {
        $list = Db::name('data_content_chip_template')->select()->toArray();
        return view('',['list'=>$list]);
    }

    public function add()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            if($param['position'] == 3){
                if(empty($param['value'])) {
                    $this->error('值不能为空');
                }
                $param['step'] = count($param['value']);
                $param['value'] = implode(',',$param['value']);
                $res = Db::name('data_content_chip_template')->where([
                    'position' => $param['position'],
                    'value' => $param['value']
                ])->find();
            }elseif($param['position'] == 4){
                if(empty($param['step'])){
                    $this->error('值不能为空');
                }
                $res = Db::name('data_content_chip_template')->where([
                    'position' => $param['position'],
                    'step' => $param['step']
                ])->find();
                $param['value'] = '';
            }
            if($res){
                $this->error('该模板已存在');
            }
            $result = Db::name('data_content_chip_template')->insert($param);
            if( $result ) {
                xn_add_admin_log('添加素材模板');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        $position2List = Db::name('data_content_chip_position')->where('pid','=',3)->order('sort asc')->select();
        return view('form',['position2List'=>$position2List]);
    }

    public function edit()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            if($param['position'] == 3){
                if(empty($param['value'])) {
                    $this->error('值不能为空');
                }
                $param['step'] = count($param['value']);
                $param['value'] = implode(',',$param['value']);
                $res = Db::name('data_content_chip_template')->where([
                    'position' => $param['position'],
                    'value' => $param['value']
                ])->find();
            }elseif($param['position'] == 4){
                if(empty($param['step'])){
                    $this->error('值不能为空');
                }
                $res = Db::name('data_content_chip_template')->where([
                    'position' => $param['position'],
                    'step' => $param['step']
                ])->find();
                $param['value'] = '';
            }
            if($res){
                $this->error('该模板已存在');
            }
            $result = Db::name('data_content_chip_template')->update($param);
            if( $result ) {
                xn_add_admin_log('修改素材模板');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }

        $id = $this->request->get('id');
        $data = Db::name('data_content_chip_template')->find($id);
        if($data['position'] == 3){
            $data['value'] = explode(',',$data['value']);
            $position2List = Db::name('data_content_chip_position')->where('pid','=',$data['position'])->order('sort asc')->select();
        }
        return view('form',['data'=>$data,'position2List'=>$position2List]);
    }

    public function delete()
    {
        $id = intval($this->request->get('id'));
        !($id>0) && $this->error('参数错误');
        Db::name('data_content_chip_template')->delete($id);
        xn_add_admin_log('删除素材模板');
        $this->success('删除成功');
    }
}
